package com.splunk.hiring.imov.controller.mvc;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Lists;
import com.splunk.hiring.imov.entity.Movie;
import com.splunk.hiring.imov.entity.dto.MovieDTO;
import com.splunk.hiring.imov.persist.db.MovieDao;
import com.splunk.hiring.imov.utils.PagedData;
import com.splunk.hiring.imov.utils.PagedData.Pager;


/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	
	@Autowired
	MovieDao movieDao;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Map<String, Object> model) {
		PagedData<Movie> movies = movieDao.getPagedMovies(new Pager(50,0));
		List<MovieDTO> movieDTOs = Lists.newArrayList();
		int index = 1;
		for(Movie m:movies.getData()){
			movieDTOs.add(new MovieDTO(index++,m));
		}
		model.put("movies", movieDTOs);
		return "imovindex";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String search(@RequestParam String genres,@RequestParam String years,Map<String, Object> model) {
		if(StringUtils.isEmpty(genres) || "undefined".equals(genres) || genres.contains("All")){genres = "";}
		if(StringUtils.isEmpty(years) ||"undefined".equals(years)|| years.contains("All")){years = "";}

		PagedData<Movie> movies = movieDao.getPagedMoviesWithYearsAndGenres(years,genres,new Pager(50,0));
		List<MovieDTO> movieDTOs = Lists.newArrayList();
		int index = 1;
		for(Movie m:movies.getData()){
			movieDTOs.add(new MovieDTO(index++,m));
		}
		model.put("movies", movieDTOs);
		model.put("condition_genres",genres);
		model.put("condition_years",years);
		return "imovindex";
	}
	
	@RequestMapping(value = "/searchname", method = RequestMethod.GET)
	public String searchName(@RequestParam String moviename,Map<String, Object> model) {
		if(StringUtils.isBlank(moviename)){
			return this.home(model);
		}
		
		PagedData<Movie> movies = movieDao.getPagedMoviesByName(moviename,new Pager(50,0));
		List<MovieDTO> movieDTOs = Lists.newArrayList();
		int index = 1;
		for(Movie m:movies.getData()){
			m.setTitle(m.getTitle().toLowerCase().replace(moviename.toLowerCase(), "<font color='red'>"+moviename+"</font>"));
			movieDTOs.add(new MovieDTO(index++,m));
		}
		model.put("movies", movieDTOs);
		model.put("movie_shortname",moviename);
		return "imovindex";
	}
	
}
